from typing import Type, Any, Iterable

from pydantic import BaseModel

from rosa.love_is_blue.backend.httpx.httpx_tools import HTTPTools
from rosa.love_is_blue.backend.memory.memory_crud import MemoryCRUDRouter
from rosa.love_is_blue.types import DEPENDENCIES, PAGINATION, GenerateRouterParams, PayloadData


class HTTPCRUDRouter(MemoryCRUDRouter):
    http = HTTPTools(backend_base_url='')

    def __init__(
            self,
            schema: Type[BaseModel],
            create_schema: Type[BaseModel] | None = None,
            update_schema: Type[BaseModel] | None = None,
            prefix: str | None = None,
            tags: list[str] | None = None,
            paginate: int | None = None,
            get_all_route: bool | DEPENDENCIES = False,
            get_one_route: bool | DEPENDENCIES = False,
            create_route: bool | DEPENDENCIES = False,
            update_route: bool | DEPENDENCIES = False,
            delete_one_route: bool | DEPENDENCIES = False,
            delete_all_route: bool | DEPENDENCIES = False,
            get_all_route_params: GenerateRouterParams | None = None,
            get_one_route_params: GenerateRouterParams | None = None,
            create_route_params: GenerateRouterParams | None = None,
            update_route_params: GenerateRouterParams | None = None,
            delete_one_route_params: GenerateRouterParams | None = None,
            delete_all_route_params: GenerateRouterParams | None = None,
            verbose_name: str = '',
            verbose_name_plural: str = '',
            delete_update_field: str = '',
            strict_authentication: bool = False,
            **kwargs: Any
    ) -> None:
        super().__init__(
            schema=schema,
            create_schema=create_schema,
            update_schema=update_schema,
            prefix=prefix,
            tags=tags,
            paginate=paginate,
            get_all_route=get_all_route,
            get_one_route=get_one_route,
            create_route=create_route,
            update_route=update_route,
            delete_one_route=delete_one_route,
            delete_all_route=delete_all_route,
            get_all_route_params=get_all_route_params,
            get_one_route_params=get_one_route_params,
            create_route_params=create_route_params,
            update_route_params=update_route_params,
            delete_one_route_params=delete_one_route_params,
            delete_all_route_params=delete_all_route_params,
            verbose_name=verbose_name,
            verbose_name_plural=verbose_name_plural,
            delete_update_field=delete_update_field,
            memory=False,
            strict_authentication=strict_authentication,
            **kwargs
        )

    # ###### 操作 ######
    async def orm_get_all(
            self,
            pagination: PAGINATION = None,
            filters: dict[str, str | list[str]] = None,
            orders: list[str] = None,
            ids: list[int] = None,
            payload: PayloadData | None = None
    ) -> tuple[list[BaseModel], int, PAGINATION]:
        """
        真正取数据的操作
        :param pagination: PAGINATION格式的页码数据
        :param filters: 字典格式，key为筛选的键，value为筛选的值
        :param orders: list类型，排序的字段名称
        :param ids: list类型，指定要刷选的多个数据id
        :param payload:
        :return: 返回一个元组，第一个元素为数据列表，第二个元素为数据总数，第三个元素为PAGINATION格式的页码数据
        """
        raise NotImplementedError

    async def orm_create(self, model_dict: dict[str, Any], other_data: dict[str, Any], payload: PayloadData | None = None) -> dict[str, Any]:
        """
        真正创建数据的操作
        :param model_dict: 创建的数据的dict类型
        :param other_data: 其他数据
        :param payload:
        :return: 创建之后数据的dict
        """
        raise NotImplementedError

    async def orm_delete_all(self, item_ids: list[int], items: Iterable[BaseModel] = None, payload: PayloadData | None = None) -> tuple[list[BaseModel], int, PAGINATION]:
        """
        真正删除所有数据的操作
        :param item_ids:
        :param items:
        :param payload:
        :return: 返回一个元组，第一个元素为数据列表，第二个元素为数据总数，第三个元素为PAGINATION格式的页码数据
        """
        raise NotImplementedError

    async def orm_get_one(self, item_id: int, payload: PayloadData | None = None) -> dict[str, Any] | None:
        """
        真正获取单个数据的操作
        :param item_id: 单个数据的id
        :param payload:
        :return: 返回单个数据的dict
        """
        raise NotImplementedError

    async def orm_update(self, item_id: int, model_dict: dict[str, Any], payload: PayloadData | None = None) -> dict[str, Any] | None:
        """
        真正更新数据的操作
        :param item_id: 操作数据的id
        :param model_dict: 更新的数据的dict
        :param payload:
        :return: 返回更新之后的数据的dict
        """
        raise NotImplementedError

    async def orm_delete_one(self, item_id: int, payload: PayloadData | None = None) -> dict[str, Any] | None:
        """
        真正删除单个数据的操作
        :param item_id: 单个数据的id
        :param payload:
        :return: 返回删除之后的数据的dict
        """
        raise NotImplementedError
